//
//  FilePathUtilities.m
//  SoMusic
//
//  Created by SoMusic on 10-7-6.
//  Copyright 2010 cc_team. All rights reserved.
//
#include "CCAll.h"

#ifdef	OBJC_MACRO

#import "CCFilePathUtilities.h"

@implementation FilePathUtilities

//获取应用程序的文档目录
+(NSString *)getDocumentDirectory
{
	NSArray *userPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentDirectory = [userPaths objectAtIndex:0];
	
	return documentDirectory;	
}

//获取应用程序文档目录中某个指定文件全路径
+ (NSString *)getFileFullPathInDocument:(NSString *)fileName
{
	NSString *docPath = [FilePathUtilities getDocumentDirectory];
	NSString *fileFullPath = [docPath stringByAppendingPathComponent:fileName];
	LOG("getFileFullPathInDocument fileFullPath is %@", fileFullPath);
	
	return fileFullPath;
}

// 获取配置文件所在路径：非越狱手机，得到的是Document目录的位置，越狱手机得到的是Document目录的上级目录
+ (NSString *)getFileFullPath:(NSString *)fileName
{
	NSString *path;
	path = [FilePathUtilities getFileFullPathInDocument:fileName];
	
	NSString *temp;
	NSFileManager *fileMgr = [NSFileManager defaultManager];
	if(![fileMgr fileExistsAtPath:path])
	{
		NSString *folder = [self getDocumentDirectory];
		temp = [self getParentFolderName:folder];
		path = [NSString stringWithFormat:@"%@/%@", temp, fileName];
	}
	
	return path;
}

// 获取目录的父目录名称
+ (NSString *)getParentFolderName:(NSString *)folderPath
{
	int i;
	for (i = folderPath.length - 2; i >= 0; i--)
	{
		if([folderPath characterAtIndex:i] == '/')
		{
			LOG("character / is at: %d", i);
			break;
		}
	}
	
	if(i == folderPath.length - 2 || i == 0)
	{
		return nil;
	}
	
	NSString *temp = [folderPath substringToIndex:i];
	return temp;
}

+ (BOOL)copySettingFileToDocumentFolderIfNotExist:(NSString *)fileName
{
	NSString *destFileFullPath = [FilePathUtilities getFileFullPathInDocument:fileName];
	NSFileManager *fileManager = [NSFileManager defaultManager];
	if(![fileManager fileExistsAtPath:destFileFullPath])
	{
		NSString *paths = [[NSBundle mainBundle] resourcePath];
		NSString *bundlePath = [paths stringByAppendingPathComponent:fileName];
		
		//将配置文件复制到Document目录下
		NSError *error = [[[NSError alloc] init] autorelease];
		BOOL ret = [fileManager copyItemAtPath:bundlePath toPath:destFileFullPath error:&error];

		return ret;
	}
	return TRUE;
}


+ (BOOL)copySettingFileToDocumentFolder:(NSString *)fileName
{
	NSString *destFileFullPath = [FilePathUtilities getFileFullPathInDocument:fileName];
	NSFileManager *fileManager = [NSFileManager defaultManager];

	NSString *paths = [[NSBundle mainBundle] resourcePath];
	NSString *bundlePath = [paths stringByAppendingPathComponent:fileName];
	
	//将配置文件复制到Document目录下
	NSError *error = [[[NSError alloc] init] autorelease];
	if(![fileManager removeItemAtPath:destFileFullPath error:&error])
	{
		return FALSE;
	}
	
	return [fileManager copyItemAtPath:bundlePath toPath:destFileFullPath error:&error];
}

+ (BOOL)deleteFileInDocFoler:(NSString *)fileName
{
	NSString *destFileFullPath = [FilePathUtilities getFileFullPathInDocument:fileName];
	NSFileManager *fileManager = [NSFileManager defaultManager];
	
	NSError *error = [[[NSError alloc] init] autorelease];
	return [fileManager removeItemAtPath:destFileFullPath error:&error];
}

+ (BOOL)deleteFileInFolder:(NSString *)folder withFileName:(NSString *)fileName
{
	NSString *destFileFullPath = [folder stringByAppendingPathComponent:fileName];
	NSFileManager *fileManager = [NSFileManager defaultManager];
	if(![fileManager fileExistsAtPath:destFileFullPath])
	{
		return FALSE;
	}
	
	NSError *error = [[[NSError alloc] init] autorelease];
	return [fileManager removeItemAtPath:destFileFullPath error:&error];
}

@end

#endif
